package com.py.credit.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.TreeObj;
import com.py.credit.service.CswhService;
import com.py.credit.service.LyService;
import com.py.framework.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 领域部门controller
 */
@Controller
@RequestMapping(value = "/ly")
public class LyController extends BaseController {

    @Autowired
    private LyService lyService;

    @Autowired
    private CswhService cswhService;

    /**
     * 右边措施列表
     *
     * @param type 奖惩类别：C:惩戒，J:激励
     * @param name 措施名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cswhTreeList.json")
    public RestServiceResult selectCswhTreeList(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                                @RequestParam(defaultValue = "", required = true, value = "name") String name) {
        List<TreeObj> treeObjs = lyService.selectCswhTreeList(type, name.trim());
        List<TreeObj> cswhTreeList = new ArrayList<TreeObj>();
        TreeObj treeObj = new TreeObj();
        TreeObj treeObj1 = new TreeObj();
        treeObj.setId("0");
        if (type.equals("C")) {
            treeObj.setName("惩戒措施列表");
        } else {
            treeObj.setName("激励措施列表");
        }
        treeObj.setType("D");
        treeObj.setpId("-1");
        treeObj.setSort(0);
        treeObj.setChildren(treeObjs);
        treeObj.setNocheck("true");
        treeObj.setIsParent("true");

        //公共措施
        List<TreeObj> treeObjs1 = lyService.selectGgCswhTreeList(type, name.trim());
        treeObj1.setId("-2");
        if (type.equals("C")) {
            treeObj1.setName("公共惩戒措施列表");
        } else {
            treeObj1.setName("公共激励措施列表");
        }
        treeObj1.setType("D");
        treeObj1.setpId("-1");
        treeObj1.setSort(0);
        treeObj1.setChildren(treeObjs1);
        treeObj1.setNocheck("true");
        treeObj1.setIsParent("true");

        cswhTreeList.add(treeObj);
      //  cswhTreeList.add(treeObj1);
        return new RestServiceResult(cswhTreeList);
    }

    /**
     * 领域列表
     *
     * @param type 奖惩类别：C:惩戒，J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lyList.json")
    public RestServiceResult getLyList(@RequestParam(defaultValue = "", required = true, value = "type") String type) {
        return new RestServiceResult(lyService.selectLyList(type));
    }

    /**
     * 左边领域树
     *
     * @param type 奖惩类别：C:惩戒，J:激励   fun:查询方式 0全部，1领域，2措施
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lyTreeList.json")
    public RestServiceResult getLyTreeList(@RequestParam(defaultValue = "", required = true, value = "type") String type,
    										@RequestParam(defaultValue = "", required = true, value = "fun") Integer fun,
                                           @RequestParam(defaultValue = "", required = true, value = "name") String name) {
    	if(fun == 1) {
    		return new RestServiceResult(lyService.selectLyTreeListByLy(type, name));
    	}else if (fun == 2) {
    		return new RestServiceResult(lyService.selectLyTreeListByCs(type, name));
    	}else if (fun == 0) {
    		return new RestServiceResult(lyService.selectLyTreeList(type, name));
    	}else {
    		
    	}
    	
        return null;
    }

    /**
     * 左边部门树
     *
     * @param type 奖惩类别：C:惩戒，J:激励       fun:查询方式 0全部，1领域，2措施
     * @return
     */

	@ResponseBody
    @RequestMapping(value = "/deptTreeList.json")
    public RestServiceResult deptTreeList(@RequestParam(defaultValue = "", required = true, value = "type") String type,
    									  @RequestParam(defaultValue = "", required = true, value = "fun") Integer fun,
                                          @RequestParam(defaultValue = "", required = true, value = "name") String name) {
    	
    	
    	if(fun == 1) {
    		return new RestServiceResult(lyService.selectDeptTreeListByDept(type, name));
    	}else if (fun == 2) {
    		return new RestServiceResult(lyService.selectDeptTreeListByMeas(type, name));
    	}else if (fun == 0) {
    		return new RestServiceResult(lyService.selectDeptTreeList(type, name));
    	}else {
    		
    	}
    	
        return null;
    }

    /**
     * 添加领域
     *
     * @param name 领域名称
     * @param code 领域编码
     * @param type 奖惩类别：C:惩戒，J:激励
     * @param sort 排序值
     * @param pId  父节点id
     * @param rId  根节点id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addLy.json")
    public RestServiceResult add(@RequestParam(defaultValue = "", required = true, value = "name") String name,
                                 @RequestParam(defaultValue = "", required = true, value = "code") String code,
                                 @RequestParam(defaultValue = "", required = true, value = "type") String type,
                                 @RequestParam(defaultValue = "", required = true, value = "sort") int sort,
                                 @RequestParam(defaultValue = "", required = true, value = "pId") int pId,
                                 @RequestParam(defaultValue = "", required = true, value = "rId") int rId) {
        if (name.trim().equals("") || name == null || code.trim().equals("") || code == null) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "分类名称或分类编码不能为空，请核对");
        } else {
            int num = lyService.selectSameCode("", name.trim(), code.trim());
            if (num > 0) {
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "存在相同的分类名称或分类编码，请核对");
            } else {
                lyService.add(name.trim(), code.trim(), type, pId, rId, sort);
                return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
            }
        }
    }

    /**
     * 删除领域
     *
     * @param type 奖惩类别：C:惩戒，J:激励
     * @param id   主键id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delLy.json")
    public RestServiceResult del(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                 @RequestParam(defaultValue = "", required = true, value = "id") String id) {
        return lyService.del(type, id);
    }

    /**
     * 修改领域
     *
     * @param id   主键id
     * @param name 领域名称
     * @param code 领域编码
     * @param type 奖惩类别：C:惩戒，J:激励
     * @param sort 排序值
     * @param pId  父节点id
     * @param rId  根节点id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyLy.json")
    public RestServiceResult modifyLy(@RequestParam(defaultValue = "", required = true, value = "id") String id,
                                      @RequestParam(defaultValue = "", required = true, value = "name") String name,
                                      @RequestParam(defaultValue = "", required = true, value = "code") String code,
                                      @RequestParam(defaultValue = "", required = true, value = "type") String type,
                                      @RequestParam(defaultValue = "", required = true, value = "sort") int sort,
                                      @RequestParam(defaultValue = "", required = true, value = "pId") int pId,
                                      @RequestParam(defaultValue = "", required = true, value = "rId") int rId) {
        if (name.trim().equals("") || name == null || code.trim().equals("") || code == null) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "分类名称或分类编码不能为空，请核对");
        } else {
            int num = lyService.selectSameCode(id, name.trim(), code.trim());
            if (num > 0) {
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "存在相同的分类名称或分类编码，请核对");
            } else {
                lyService.update(id, name.trim(), code.trim(), type, pId, rId, sort);
                return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
            }
        }
    }

    /**
     * 查询领域
     *
     * @param id 主键id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getLy.json")
    public RestServiceResult getLy(@RequestParam(defaultValue = "", required = true, value = "id") String id) {
        return new RestServiceResult(lyService.selectLy(id));
    }

    /**
     * 添加措施领域
     *
     * @param type     奖惩类别：C:惩戒，J:激励
     * @param lyId     领域id
     * @param csIdList 措施id(多个逗号拼接)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCsLy.json")
    public RestServiceResult addCsLy(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                     @RequestParam(defaultValue = "", required = true, value = "lyId") String lyId,
                                     @RequestParam(defaultValue = "", required = true, value = "csIdList") String csIdList
    ) {
        return cswhService.addCsLy(type, lyId, csIdList);
    }

    /**
     * 删除措施领域
     *
     * @param lyCsList
     * @return
     */
    @ResponseBody
    @RequestMapping("/delCsLy.json")
    public RestServiceResult delCsLy(@RequestParam(defaultValue = "", required = true, value = "lyCsList") String lyCsList) {
        //json解析字符串
        JSONArray lyCsArray = JSON.parseArray(lyCsList);
        for (int i = 0; i < lyCsArray.size(); i++) {
            JSONObject lyCs = lyCsArray.getJSONObject(i);
            cswhService.delCsLy(lyCs.getString("ly"), lyCs.getString("cs"));
        }

        return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
    }

    /**
     * 添加措施部门
     *
     * @param type     奖惩类别：C:惩戒，J:激励
     * @param deptId   部门id
     * @param csIdList 措施id(多个逗号拼接)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCsDept.json")
    public RestServiceResult addCsDept(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                       @RequestParam(defaultValue = "", required = true, value = "deptId") String deptId,
                                       @RequestParam(defaultValue = "", required = true, value = "csIdList") String csIdList) {
        return cswhService.addCsDept(type, deptId, csIdList);
    }

    /**
     * 删除措施部门
     *
     * @param deptCsList
     * @return
     */
    @ResponseBody
    @RequestMapping("/delCsDept.json")
    public Object delCsDpet(@RequestParam(defaultValue = "", required = true, value = "deptCsList") String deptCsList) {
        //json解析字符串
        JSONArray deptCsArray = JSON.parseArray(deptCsList);
        for (int i = 0; i < deptCsArray.size(); i++) {
            JSONObject deptCs = deptCsArray.getJSONObject(i);
            cswhService.delCsDept(deptCs.getString("dept"), deptCs.getString("cs"));
        }

        return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
    }

    /**
     * Excel统计导出
     *
     * @param response
     * @param type     奖惩类别：C:惩戒，J:激励
     */
    @ResponseBody
    @RequestMapping(value = "/exportExcel.xls")
    public void ExcelExport(HttpServletResponse response,
                            @RequestParam(value = "type", defaultValue = "", required = true) String type) {
        lyService.excelExport(response, type);
    }
}
